package com.yuwubao.hz_International.services.impl;

import com.yuwubao.hz_International.entities.StoreEntity;
import com.yuwubao.hz_International.entities.repository.StoreRepository;
import com.yuwubao.hz_International.entities.vo.StoreVo;
import com.yuwubao.hz_International.services.StoreService;
import org.apache.commons.lang3.StringUtils;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by yangyu on 2018/1/25.
 */
@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    private StoreRepository storeRepository;

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<StoreVo> findStoreList(String storeName, int index, int size) {
        String sql = "SELECT s.id, s.`name`, c.name AS consumerName, c.createTime, c.money,s.stick FROM consumer c, store s WHERE c.id = s.consumerId ";
        if (StringUtils.isNotBlank(storeName)) {
            sql += " AND s.name LIKE '%" + storeName + "%'";
        }
        sql += " order by stick desc, stickTime DESC ";
        if (size != 0) {
            if (index == 0) {
                sql += " LIMIT " + index + "," + size;
            } else {
                sql += " LIMIT " + index*size + "," + size;
            }
        }
        RowMapper<StoreVo> rowMapper = new BeanPropertyRowMapper<>(StoreVo.class);
        List<StoreVo> list = jdbcTemplate.query(sql, rowMapper);
        return list;
    }

    @Override
    public StoreEntity findOne(int storeId) {
        return storeRepository.findOne(storeId);
    }

    @Override
    public StoreEntity update(StoreEntity storeEntity) {
        StoreEntity entity = storeRepository.findOne(storeEntity.getId());
        if (entity != null) {
            ModelMapper model = new ModelMapper();
            StoreEntity newEntity = model.map(storeEntity, StoreEntity.class);
            return storeRepository.save(newEntity);
        }
        return null;
    }

    @Override
    public StoreEntity delete(int storeId) {
        StoreEntity storeEntity = storeRepository.findOne(storeId);
        if (storeEntity != null) {
            storeRepository.delete(storeId);
            return storeEntity;
        }
        return null;
    }

    @Override
    public StoreEntity save(StoreEntity storeEntity) {
        return storeRepository.save(storeEntity);
    }

    @Override
    public StoreEntity findByConsumerId(int consumerId) {
        return storeRepository.findByConsumerId(consumerId);
    }

    @Override
    public void stick(int id) {
        StoreEntity store = storeRepository.findOne(id);
        int stick = store.getStick() ==0 ? 1:0;
        store.setStick(stick);
        Date time = null;
        if(stick==1){
            time = new Date();
        }
        store.setStickTime(time);
        storeRepository.save(store);
    }
}
